const { Controller } = require('egg')
const dayjs = require('dayjs')

class ApiController extends Controller {
  /**
   * @api {get} /person/enter [人员] 返/入数据
   * @apiGroup Person
   * @apiDescription 态势总览-入西返西登记趋势
   * 
   * @apiParam {String} [scope=day] 统计范围 week（周）、month（月）
   * 
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "success",
      "data": [
          {
              "label": "2022-12-04",
              "value": 35
          }
      ]
  }
   */
  async enterPerson() {
    const { ctx } = this
    const { query } = ctx
    const scope = query.scope || 'week'

    const returnData = await ctx.model.TrackData.find({ trackName: `${scope}_person_enter` })
      .limit(7)
      .then(res => {
        return res.map(item => {
          let label

          if (scope == 'month') {
            label = dayjs(item.trackDate).format('YYYY-MM')
          } else {
            label = dayjs(item.trackDate).format('YYYY-MM-DD')
          }

          return {
            label,
            value: item.total,
          }
        })
      })

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }

  /**
   * @api {get} /person/positive [人员] 阳性人员
   * @apiGroup Person
   * @apiDescription 核酸检测-阳性数据统计
   * 
   * 
   * @apiSuccessExample {json} Success-Response:
    {
        "code": 0,
        "msg": "success",
        "data": [
            {
                "total": 35, // 累计确诊
                "jzgl": 35, // 集中隔离
                "jjgl": 0, // 居家隔离
                "new": 1, // 当日新增
                "shsc": 0, // 社会筛查
                "zdjz": 0, // 主动就诊
                "label": "包屯镇" // 乡镇
                "zdry": 13,// 重点人员
            }
        ]
    }
   */
  async positivePerson() {
    const { ctx } = this

    const returnData = await this.service.trackData.getData(`town_positive`)

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData.list,
    }
  }

  /**
   * @api {get} /person/town [人员] 乡镇人员
   * @apiGroup Person
   * @apiDescription 态势总览-人口信息 \ 人员信息-各乡镇人口分布
   * 
   * 
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "success",
      "data": [
          {
              "total": 21229,// 总数
              "cz": 0, // 常住人口
              "fcz": 0, // 非常住人口
              "label": "固城乡" // 乡镇名称
              "percent": 100, // 常住人口百分比
          },
      ]
  }
   */
  async townPerson() {
    const { ctx } = this
    const { query } = ctx

    const returnData = await this.service.trackData.getData(`town_person`)

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }

  /**
   * @api {get} /person/probably [人员] 密接人员
   * @apiGroup Person
   * @apiDescription 核酸检测-密接人员数据
   * 
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "success",
      "data": [
          {
              "name": "吴俊河", // 姓名
              "address": "包屯镇", // 所在街道
              "relation": "夫妻", // 与阳性人员关系
              "handle": "" // 处理方式
          }
        ]
  }
   */
  async probablyPerson() {
    const { ctx } = this

    const returnData = await this.service.trackData.getData(`person_probably`)

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }

  /**
   * @api {get} /person/from-city [人员] 来源地
   * @apiGroup Person
   * @apiDescription 返乡报备-返乡人员来源地分析  
   * 
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "success",
      "data": {
          // 省内城市
          "province": [
              {
                  "value": 9,
                  "label": "周口市"
              },
          ],
          // 省外城市
          "otherProvince": [
              {
                  "value": 9,
                  "label": "上海市"
              },
              {
                  "value": 8,
                  "label": "广州市"
              },
              {
                  "value": 10,
                  "label": "成都市"
              },
              {
                  "value": 8,
                  "label": "深圳市"
              }
          ]
      }
  }
   */
  async fromCity() {
    const { ctx } = this
    const { query } = ctx
    const scope = query.scope || 'day'

    const returnData = await this.service.trackData.getData(`${scope}_person_from_city`)

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }

  /**
   * @api {get} /person/age-group [人员] 年龄段
   * @apiGroup Person
   * @apiDescription 人口信息-人口特征结构
   * 
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "success",
      "data": [
          {
              "label": "0-20岁",
              "value": 223303
          },
          {
              "label": "20-40岁",
              "value": 1
          },
          {
              "label": "40-60岁",
              "value": 0
          },
          {
              "label": "60岁以上",
              "value": 6
          }
      ]
  }
   */
  async ageGroup() {
    const { ctx } = this

    const returnData = await this.service.trackData.getData(`person_age_group`)

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }

  /**
   * @api {get} /person/special [人员] 特殊人员
   * @apiGroup Person
   * @apiDescription 人口信息-特殊人群信息
   * 
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "success",
      "data": [
          {
              "label": "  扶沟县", // 乡镇名称
              "djlr": 0, // 独居老人
              "jszz": 0, // 精神智障
              "cqwc": 0, // 长期卧床
              "cjr": 0, // 残疾人
              "jcbhz": 0, // 继承病患者
              "xytxhz": 0, // 血液透析患者
              "total": 0 // 特殊人群总数
          }
      ]
  }
   */
  async specialPerson() {
    const { ctx } = this

    const returnData = await this.service.trackData.getData(`town_special_person`)

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }

  /**
   * @api {get} /person/type [人员] 分类
   * @apiGroup Person
   * @apiDescription 人口信息-全县人口信息
   * 
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "success",
      "data": {
          "cz": 9, // 长住人口
          "zh": 10, // 租户数
          "wcwg": 4, // 外出务工
          "wcjs": 1, // 外出精神 
          "wcsx": 2, // 外出上学
          "tsrq": 15 // 特殊人群数
      }
  }
   */
  async personType() {
    const { ctx } = this

    const returnData = await this.service.trackData.getData(`person_type`)

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }

  /**
   * @api {get} /person/enter-trend [人员] 流入趋势
   * @apiGroup Person
   * @apiDescription 人口信息-返乡外来人口趋势
   * 
   * @apiParam {String} [scope=day] 统计范围 week（周）、month（月）
   * 
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "success",
      "data": [
          {
              "label": "2022-12-04",
              "value": 35
          }
      ]
  }
   */
  async enterTrend() {
    const { ctx } = this
    const { query } = ctx
    const scope = query.scope || 'week'

    const returnData = await ctx.model.TrackData.find({ trackName: `${scope}_person_enter_trend` })
      .limit(7)
      .then(res => {
        return res.map(item => {
          let label

          if (scope == 'month') {
            label = dayjs(item.trackDate).format('YYYY-MM')
          } else {
            label = dayjs(item.trackDate).format('YYYY-MM-DD')
          }

          return {
            label,
            value: item.total,
          }
        })
      })

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }
}

module.exports = ApiController
